//
//  CartoonClassifyHotViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CartoonClassifyHotViewController.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "MJRefresh.h"
#import "CartoonClassifyHotCell.h"
#import "CartoonClassifyHotModel.h"
#import "CartoonRecommendDetailViewController.h"
#import "ADCustomTableView.h"
@interface CartoonClassifyHotViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *bigArray;
@property (nonatomic, assign) int i;

@end
@implementation CartoonClassifyHotViewController
- (void)dealloc
{
    [_myID2 release];
    [_bigArray release];
    [_table1 release];
    
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
        self.i = 2;
        
    }
    return self;
}

#pragma mark -
#pragma mark 请求数据
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=listByType&id=%@&page=1&limit=30&order=2", self.myID2];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSArray *bigArray = [dic objectForKey:@"list"];
        for (NSDictionary *dic in bigArray) {
            CartoonClassifyHotModel *model = [[CartoonClassifyHotModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)MyTableView
{
    self.table1 = [[ADCustomTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    //设置代理人
    _table1.dataSource = self;
    _table1.delegate = self;
    self.table1.separatorStyle = UITableViewCellStyleDefault;
    //设定tableView重用池
    [self.table1 registerClass:[CartoonClassifyHotCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table1];
    [self.table1 addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    [self.table1 addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [_table1 release];
}
//下拉刷新点击事件
- (void)loadNewData:(id)sender
{

    [self.table1 reloadData];

    [self.table1.header endRefreshing];}
//上啦加载点击事件
- (void)loadMoreData
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=listByType&id=%@&page=%d&limit=30&order=1", self.myID2, self.i];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        NSArray *bigArray = [dic objectForKey:@"list"];
        if (bigArray.count == 0) {
            [self.table1 removeFooter];
            return;
        }
        for (NSDictionary *dic in bigArray) {
            CartoonClassifyHotModel *model = [[CartoonClassifyHotModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        [self.table1 reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    // 拿到当前的上拉刷新控件，结束刷新状态
    
    self.i += 1;
    [self.table1.footer endRefreshing];
    
}

#pragma mark -
#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.bigArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CartoonClassifyHotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CartoonClassifyHotModel *model = [self.bigArray objectAtIndex:indexPath.row];
    //    [cell.image setImage:model.pic];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    [cell.lable1 setText:model.name];
    
    [cell.lable3 setText:[NSString stringWithFormat:@"%@分" ,model.score]];
    //判断是否连载
    if (model.is_over == 1) {
        
        [cell.lable2 setText:[NSString stringWithFormat:@"全%@集" ,model.cur_num]];
        
    } else {
        [cell.lable2 setText:[NSString stringWithFormat:@"更新到第%@集" ,model.cur_num]];
    }
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
    CartoonClassifyHotModel *model = [self.bigArray objectAtIndex:indexPath.row];
    cartoon.iD =  model.video_id;
    cartoon.is_over = [NSString stringWithFormat:@"%ld", (long)model.is_over];
    cartoon.lastup = model.cur_num;
    cartoon.titleName = model.name;
    [self.navigationController pushViewController:cartoon animated:YES];
    [cartoon release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self MyTableView];
    [self startConnection];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
